package com.module.app.widget.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;

import com.module.app.bean.IType;
import com.module.app.bean.IType;
import com.module.base.cache.CacheSDK;
import com.module.app.R;


/**
 * @author bsnl_yanxuwen
 * @date 2021/4/14 16:16
 * Description :
 */
public class CommonDialog extends BaseDialog {


    private TextView tv_title;
    private TextView tv_content;
    private TextView tv_submit;
    private TextView tv_cancel;
    private View layout_content;

    private AlertBuilder builder;

    public CommonDialog(@NonNull Context context) {
        super(context);
    }

    public CommonDialog(@NonNull AlertBuilder builder) {
        super(builder.context);
        this.builder = builder;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setAnimResId(R.style.showDialog);
        super.onCreate(savedInstanceState);
        tv_title = rootView.findViewById(R.id.tv_title);
        tv_content = rootView.findViewById(R.id.tv_content);
        tv_submit = rootView.findViewById(R.id.tv_submit);
        tv_cancel = rootView.findViewById(R.id.tv_cancel);
        layout_content = rootView.findViewById(R.id.layout_content);

        if (builder != null && !TextUtils.isEmpty(builder.title)) {
            tv_title.setText(builder.title);
        }
        if (builder != null && !TextUtils.isEmpty(builder.content)) {
            tv_content.setText(builder.content);
        }
        if (builder != null && !TextUtils.isEmpty(builder.cancelText)) {
            tv_cancel.setText(builder.cancelText);
            tv_cancel.setVisibility(View.VISIBLE);
        }
        if (builder != null && !TextUtils.isEmpty(builder.confirmText)) {
            tv_submit.setText(builder.confirmText);
        }
        tv_submit.setOnClickListener(view -> {
            onSubmitClick(view);
        });
        tv_cancel.setOnClickListener(view -> {
            if (builder != null && builder.mOnCancelListener != null) {
                builder.mOnCancelListener.onClick(view);
            }
            if (builder != null && builder.isAutoDismiss) {
                dismiss();
            }
        });

        tv_content.setGravity(builder.gravity);
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (builder.mCanceledOnTouchOutside) {
                    dismiss();
                }
            }
        });
    }

    public void setContent(CharSequence title) {
        tv_content.setText(title);
    }

    public static class Builder {
        protected AlertBuilder mController;

        public Builder(Context context) {
            mController = new AlertBuilder();
            mController.context = context;

        }

        public Builder setTitle(@StringRes int titleId) {
            mController.title = mController.context.getResources().getString(titleId);
            return this;
        }

        public Builder setTitle(CharSequence title) {
            mController.title = title;
            return this;
        }

        public Builder setContent(@StringRes int titleId) {
            mController.content = mController.context.getResources().getString(titleId);
            return this;
        }

        public Builder setContent(CharSequence title) {
            mController.content = title;
            return this;
        }

        public Builder setCancelButton(@StringRes int titleId) {
            mController.cancelText = mController.context.getResources().getString(titleId);
            return this;
        }

        public Builder setCancelButton(CharSequence title) {
            mController.cancelText = title;
            return this;
        }

        public Builder setConfirmButton(@StringRes int titleId) {
            mController.confirmText = mController.context.getResources().getString(titleId);
            return this;
        }

        public Builder setConfirmButton(CharSequence title) {
            mController.confirmText = title;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            mController.mCancelable = cancelable;
            return this;
        }

        public Builder setAutoDismiss(boolean isAutoDismiss) {
            mController.isAutoDismiss = isAutoDismiss;
            return this;
        }

        public Builder setCanceledOnTouchOutside(boolean mCanceledOnTouchOutside) {
            mController.mCanceledOnTouchOutside = mCanceledOnTouchOutside;
            return this;
        }

        public Builder setOnCancelListener(View.OnClickListener onCancelListener) {
            mController.mOnCancelListener = onCancelListener;
            return this;
        }

        public Builder setOnConfirmListener(View.OnClickListener mOnConfirmListener) {
            mController.mOnConfirmListener = mOnConfirmListener;
            return this;
        }

        /**
         * @see android.view.Gravity
         */
        public Builder setContentGravity(int gravity) {
            mController.gravity = gravity;
            return this;
        }

        public Builder setCacheStr(String cacheStr) {
            mController.cacheStr = cacheStr;
            return this;
        }

        public Builder setOnDismissListener(OnDismissListener onDismissListener) {
            mController.mOnDismissListener = onDismissListener;
            return this;
        }

        public CommonDialog create() {
            // Context has already been wrapped with the appropriate theme.
            final CommonDialog dialog = new CommonDialog(mController);
            dialog.setCancelable(mController.mCancelable);
            dialog.setOnDismissListener(mController.mOnDismissListener);
            return dialog;
        }

        public void show() {
            create().show();
        }

        public void show(long delayMillis) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mController.context == null || (mController.context instanceof Activity && ((Activity) mController.context).isFinishing())) {
                        return;
                    }
                    create().show();
                }
            }, delayMillis);
        }
    }


    @Override
    public int getLayoutId() {
        return R.layout.app_dialog_common;
    }


    @Override
    public void show() {
        if (builder != null && !TextUtils.isEmpty(builder.cacheStr)) {
            boolean isShow = CacheSDK.get(IType.ICache.DIALOG_SHOW + builder.cacheStr, Boolean.class);
            if (isShow) {
                //显示过了,默认点击确定按钮
                onSubmitClick(null);
                return;
            }
        }
        super.show();
    }

    private void onSubmitClick(View view) {
        if (builder != null && builder.mOnConfirmListener != null) {
            builder.mOnConfirmListener.onClick(view);
        }
        if (builder != null && builder.isAutoDismiss) {
            dismiss();
        }
        if (builder != null && !TextUtils.isEmpty(builder.cacheStr)) {
            CacheSDK.put(IType.ICache.DIALOG_SHOW + builder.cacheStr, true);
        }
    }
}
